<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="售后单号" prop="afterCode">
        <el-input
          v-model="queryParams.afterCode"
          placeholder="请输入售后单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="售后类型" prop="type">
        <el-select v-model="queryParams.type" clearable filterable  placeholder="请选择售后类型">
          <el-option
            v-for="item in dict.type.after_sales_type"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="售后状态" prop="afterStatus">
        <el-select v-model="queryParams.afterStatus" clearable filterable  placeholder="请选择售后状态">
          <el-option
            v-for="item in dict.type.after_sales_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="运营商" prop="operatorUserId" v-if="getoperatorfunc(this).userType != 1">
        <el-select v-model="queryParams.operatorUserId" placeholder="运营商" filterable clearable style="width: 200px">
          <el-option
            v-for="dict in operatorList"
            :key="dict.id"
            :label="dict.label"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="售后单号" align="center" prop="afterCode" />
      <el-table-column label="运营商名称" align="center" prop="operatorName" v-if="getoperatorfunc(this).userType != 1" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="购买数量" align="center" prop="count" />
      <el-table-column label="审批时间" align="center" prop="auditTime" />
      <el-table-column label="审批人" align="center" prop="auditUserId" />
      <el-table-column label="退款金额" align="center" prop="refundPrice" />
      <el-table-column label="退款时间" align="center" prop="refundTime" />
      <el-table-column label="售后类型" align="center" prop="type" >
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.after_sales_type" :value="row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="售后状态" align="center" prop="afterStatus" >
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.after_sales_status" :value="row.afterStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="{row}">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleDetail(row,1)"
            v-hasPermi="['trade:serverAfterSale:query']"
          >查看详情</el-button>
          <el-dropdown size="mini" @command="(command) => handleCommand(command, row)" v-if="getCaozuo(row).length"
          >
            <el-button size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-for="(item,index) in getCaozuo(row)" :key="index" :command="item.command" :icon="item.icon"
              >{{item.label}}
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row class="diy-text-sm diy-leading-10" :gutter="10" v-if="title == '查看详情'">
          <el-col :span="item.span || 12" v-for="(item,index) in formDetail" :key="index">
            <div class="flex_hcneter">
              <svg-icon icon-class="dot" class-name='diy-text-primary diy-mr-1'/>
              <div v-if="item.zidian && item.isCustom" class="flex_hcneter">
                {{item.label}}：<dict-tag :options="dict.type[item.zidian]" :value="form[item.prop]"/>
              </div>
              <div v-else-if="item.type == 'textarea'">
                {{item.label}}：
                <div v-html="form[item.prop]"></div>
              </div>
              <div v-else-if="item.type == 'image'" class="flex_hcneter" style="margin-bottom:10px;">
                {{item.label}}：
                <el-image v-if="form[item.prop]"
                  style="width: 100px; height: 100px;"
                  :src="form[item.prop]"
                  :preview-src-list="[form[item.prop]]">
                </el-image>
              </div>
              <div v-else>{{item.label}}：{{ form[item.prop] }}{{ item.danwei || '' }}</div>
            </div>
          </el-col>
        </el-row>
        <div v-else-if="title == '驳回'">
          <el-form-item label="驳回原因" prop="auditReason">
            <el-input v-model="form.auditReason" type="textarea" rows="6" clearable placeholder="请输入驳回原因"/>
          </el-form-item>
        </div>
        <div v-else>
          <el-form-item label="真实姓名" prop="name">
            <el-input v-model="form.name" clearable placeholder="请输入真实姓名"/>
          </el-form-item>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-if="title != '查看详情'" :loading="popLodding" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listServerAfterSale, getServerAfterSale, approve, reject, } from "@/api/commodity/serviceAfterSales";
import { listAllOperator } from "@/api/operator/operatorList";
export default {
  name: "ServiceAfterSales",
  dicts: ['after_sales_type','after_sales_status',],
  components: { },
  data() {
    return {
      popLodding:false,//弹窗加载
      formDetail:[//表单
        {label:'售后单号',prop:'afterCode',},{label:'订单号',prop:'orderNo',},{label:'商品 SPU 名称',prop:'spuName',},
        {label:'商品图片',prop:'picUrl',span:24,type:'image',},
        {label:'购买数量',prop:'count',},
        {label:'退款金额',prop:'refundPrice',},{label:'支付退款编号',prop:'payRefundId',},{label:'退款时间',prop:'refundTime',},
        {label:'审批时间',prop:'auditTime',},{label:'审批人',prop:'auditUserId',},{label:'审批备注',prop:'auditReason',span:24,type:'textarea',},
        {label:'审核时间',prop:'auditTime',},{label:'申请原因',prop:'applyReason',span:24,type:'textarea',},
        {label:'售后类型',prop:'type',zidian:'after_sales_type',isCustom:true,},{label:'售后状态',prop:'afterStatus',zidian:'after_sales_status',isCustom:true,},
      ],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,afterCode: undefined,type: undefined,afterStatus: undefined,
        pageSize: 10,operatorUserId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        auditReason: [ { required: true, message: "驳回原因不能为空", trigger: "blur" } ],
      },
      tgPermission:'trade:serverAfterSale:approve ',//通过
      bhPermission:'trade:serverAfterSale:approve ',//驳回
      operatorList:[],//运营商数据
    };
  },
  created() {
    this.listAllOperator()
    this.getList();
  },
  methods: {
    async listAllOperator(){//获取运营商
      let res =await listAllOperator()
      let data = res.data || []
      this.operatorList = data
    },
    getCaozuo(row){//获取操作按钮
      const user = this.$store.state.user
      const permissions = user.permissions
      const flag = !!permissions.find(item => item == this.tgPermission)
      const flag1 = !!permissions.find(item => item == this.bhPermission)
      let caozuoList = []
      if( row.afterStatus === 0 && (flag || permissions[0] == '*:*:*') ) {
        caozuoList.push({label:'通过审核',command:'handleAuith',icon:'',})
      }
      if( row.afterStatus === 0 && (flag1 || permissions[0] == '*:*:*')) {
        caozuoList.push({label:'驳回申请',command:'handlebohui',icon:'',})
      }
      return caozuoList
    },
    // 更多操作触发
    handleCommand(command, row) {
      switch (command) {
        case "handleAuith":
          this.handleAuith(row);
          break;
        case "handlebohui":
          this.handlebohui(row);
          break;
      }
    },
    /** 查列表 */
    getList() {
      this.loading = true;
      let queryParams = {...this.queryParams}
      listServerAfterSale(queryParams).then(response => {
        let rows = response.rows || [];
        this.list = rows.map(item=>{
          return {
            ...item,
            refundPrice:this.math.divide(item.refundPrice || 0,100),
          }
        })
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 详情按钮操作 */
    handleDetail(row,type) {
      const id = row.id
      getServerAfterSale(id).then(response => {
        let data =  response.data || {};
        this.form = {
          ...data,
          refundPrice:this.math.divide(data.refundPrice || 0,100),
        }
        this.open = true;
        this.title = type == 2 ? '修改佣金提现' : "查看详情";
      });
    },
    handlebohui(row) {//驳回
      this.open = true;
      this.title = '驳回'
      this.form = {id:row.id,}
    },
    /** 审核按钮操作 */
    handleAuith(row) {
      const id = row.id
      this.$modal.confirm(`是否确认审核通过该数据项？`).then(function() {
        return approve({id});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let form = {...this.form}
          this.popLodding = true
          reject(form).then(response => {
            this.popLodding = false
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          }).catch(error=>this.popLodding = false);
        }
      });
    },
  }
};
</script>